package Person;

import java.util.ArrayList;
import java.util.List;

/**
 * 过滤器模式
 * @author Cjj
 */
public class CriteriaMale implements Criteria{
    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> people = new ArrayList<Person>();
        for (Person person : persons) {
            if(person.getGender().equalsIgnoreCase("MALE")){
                people.add(person);
            }
        }
        return people;
    }

public class CriteriaFemale implements Criteria{

    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> meetCriteria = new ArrayList<Person>();
        for (Person person : persons) {
            if(person.getGender().equalsIgnoreCase("FEMALE")){
                meetCriteria.add(person);
            }
        }
        return meetCriteria;
    }
}


public class CriteriaSingle implements Criteria{

    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> meetCriteria = new ArrayList<Person>();
        for (Person person : persons) {
            if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){
                meetCriteria.add(person);
            }
        }
        return meetCriteria;
    }
}

 public class AndCriteria implements Criteria{

    private Criteria criteria;
    private Criteria otherCriteria;

    public AndCriteria(Criteria criteria, Criteria otherCriteria) {
        this.criteria = criteria;
        this.otherCriteria = otherCriteria;
    }

    @Override
     public List<Person> meetCriteria(List<Person> persons) {
        List<Person> fristCriteriaItems = criteria.meetCriteria(persons);
        List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);

        for (Person person : otherCriteriaItems) {
            if(!fristCriteriaItems.contains(person)){
                fristCriteriaItems.add(person);
            }
        }
        return fristCriteriaItems;
    }
    

 }
}
